﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Xml;
using AMS.Web.WorkBill;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.Report
{
    public partial class IdlenessOfAssetReport : System.Web.UI.Page
    {
        private readonly CastleAccess m_Acc = new CastleAccess();
        private string sql = "";
        private ObjXmlReader m_XmlReader;
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private PageBuilder m_PageBuilder;
        private WoPageBuilder m_WoPageBuilder;

        private string QueryCondition
        {
            get
            {

                if (ViewState["QueryCondition"] != null)
                    return ViewState["QueryCondition"].ToString();
                else
                    return "";
            }
            set { ViewState["QueryCondition"] = value; }
        }

        private string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Report\\ReportDataCollectSetting.xml"; }
        }

        private string WorkBillSettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\WoSetting.xml"; }
        }

      
        public string MenuId
        {
            get { return Request.QueryString["MenuId"]; }
        }

        private string ClassName
        {
            get { return Request.QueryString["ClassName"]; }
        }

     

        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this, SettingXmlPath);
            if (m_WoPageBuilder == null)
                m_WoPageBuilder = new WoPageBuilder(Page, WorkBillSettingXmlPath);
                m_PageBuilder.SetPgListAttribute(ListGridInfo, ClassName);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            ListGridInfo.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
            if (Request.Cookies["Code"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                m_CreateCtlHlp.SetDdlCondition(ListGridInfo, ddlCondition);
                m_XmlReader = new ObjXmlReader(SettingXmlPath);
                XmlNode node = m_XmlReader.GetXmlNode("Setting/" + ClassName);
           }
        }



        private void ListBind()
        {
            ListGridInfo.BindGrid();

        }

        private object BindPgList()
        {
            m_XmlReader = new ObjXmlReader(SettingXmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/" + ClassName + "/ListSql");
            if (node != null)
            {
                sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                sql += " " + QueryCondition;
                DataTable dt = m_Acc.GetDataTableBySql(sql);
                return dt;
            }
            return null;
        }





        protected void btnQuery_Click1(object sender, EventArgs e)
        {
           
          QueryCondition = QueryHelper.GetQueryCondition(ddlCondition.SelectedValue, txtValue.Text, rbtAmount.Checked);
          ListBind();
        }

        protected void ConditionSelector1_SelectCondition(SelectConditionEventArgs e)
        {
            QueryCondition = null;
            if (ConditionSelector1.SelectedConditionString.Length > 0)
                QueryCondition = " where " + ConditionSelector1.SelectedConditionString;
            ListBind();
        }

     
        protected void ListGridInfo_OnGridItemSelect(object sender, PaginationGridItemSelectArgs e)
        { 
           m_WoPageBuilder.ShowAssetInfoByAssetId(e.SelectedDataKeyValue, Page);
        }
    }
}
